<template>
  <div class="star" :class="starType">
    <span v-for="(itemClass, index) in itemClasses" :class="itemClass" class="star-item" :key="index"></span>
  </div>
</template>

<script>
const LENGTH = 5
const CLS_ON = 'on'
const CLS_HALF = 'half'
const CLS_OFF = 'off'

export default {
  name: 'assess1',
  data () {
    return {
      size: 48,
      score: 3.6
    }
  },
  computed: {
    starType () {
      return 'star-' + this.size
    },
    itemClasses () {
      let result = []
      let score = Math.floor(this.score * 2) / 2
      let hasDecimal = score % 1 !== 0
      let integer = Math.floor(score)
      for (let i = 0; i < integer; i++) {
        result.push(CLS_ON)
      }
      if (hasDecimal) {
        result.push(CLS_HALF)
      }
      while (result.length < LENGTH) {
        result.push(CLS_OFF)
      }
      return result
    }
  }
}
</script>
<style scoped>
.star{
  width: 100%;
  height: 100%;
}
.star-item{
display: inline-block;
background-repeat: no-repeat;
}
span.star-item.on{
width: 18px;
height: 12px;
line-height: 18px;
margin-right: 2px;
background-size: 12px 12px;
background-image: url('../common/img/resource/img/star48_on@2x.png');
}
span.star-item.off{
width: 18px;
height: 12px;
line-height: 18px;
margin-right: 2px;
background-size: 12px 12px;
background-image: url('../common/img/resource/img/star48_off@2x.png');
}
span.star-item.half{
width: 18px;
height: 12px;
line-height: 18px;
margin-right: 2px;
background-size: 12px 12px;
background-image: url('../common/img/resource/img/star48_half@2x.png');
}
</style>
